package com.kfm.dao;

import com.kfm.model.Dept;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class DeptDao {

    private Connection getConn() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/kfm", "root", "");
    }

    private QueryRunner runner = new QueryRunner();
    public List<Dept> selectAll() throws SQLException {
        String sql = "select * from s_dept";
        return runner.query(getConn(), sql, new BeanListHandler<>(Dept.class));
    }

}
